package com.v2future.sjt.collegecheckingattendance.leave;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.leave.adapter.TeacherListAdapter;
import com.v2future.sjt.collegecheckingattendance.leave.bean.TeacherBean;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.DensityUtil;
import com.v2future.sjt.collegecheckingattendance.utils.IntentKey;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;
import com.v2future.sjt.collegecheckingattendance.widget.view.PullToRefreshView;

import java.io.Serializable;
import java.util.List;

/**
 * 请课时假选择教师页面
 */
public class CourseLeaveTeacherActivity extends MainActivity implements PullToRefreshView.OnHeaderRefreshListener,
        PullToRefreshView.OnFooterRefreshListener{

    PullToRefreshView pullView;
    ListView teacherLv;
    TeacherListAdapter teacherListAdapter;
    List<TeacherBean> dataList;

    @Override
    public void init() {

    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_course_leave_teacher);
        setTitleText("申请课时假老师");
        initTitleBack(null);
        pullView = (PullToRefreshView) findViewById(R.id.pullView);
        pullView.setOnHeaderRefreshListener(this);
        pullView.setOnFooterRefreshListener(this);
        pullView.setFooterViewVisibility(View.GONE);
        teacherLv = (ListView) findViewById(R.id.lv_teachers);
    }

    @Override
    public void initAfter() {
        showTitleLoading();
        doGetTeacherList();
    }

    private void refreshView(){
        if (null == dataList || dataList.size() == 0) {
            tip("暂无教师可以选择");
            return;
        }
        teacherListAdapter = new TeacherListAdapter(this, dataList);
        teacherLv.setAdapter(teacherListAdapter);
        teacherLv.setOnItemClickListener(teacherListClickListener);
    }

    private void doGetTeacherList(){
        BaseRequestParams params = new BaseRequestParams(this, formatUrl(Constants.TEACHER_LIST));
        NetUtils.doGetRequest(this, params, new NetTaskListener<TeacherListResponseBean>() {

            @Override
            public void onSuccess(TeacherListResponseBean result) {
                dataList = result.getData();
                refreshView();
            }

            @Override
            public void onError() {

            }

            @Override
            public void onInterError(TeacherListResponseBean result) {
                tip(result.getFailReason());
            }

            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            @Override
            public void onCancelled() {

            }

            @Override
            public void onFinished() {
                cancelLoadingDialog();
                cancelTitleLoading();
                pullView.onHeaderRefreshComplete();
            }

            @Override
            public void onSuccessCache(TeacherListResponseBean result) {

            }
        });
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {

    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        doGetTeacherList();
    }

    AdapterView.OnItemClickListener teacherListClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TeacherBean bean = dataList.get(position);
            Intent intent = new Intent(CourseLeaveTeacherActivity.this, CourseLeaveActivity.class);
            intent.putExtra(IntentKey.TEACHER, bean);
            startActivity(intent);
        }
    };

    public class TeacherListResponseBean extends BaseResponseBean implements Serializable{
        public List<TeacherBean> data;

        public List<TeacherBean> getData() {
            return data;
        }

        public void setData(List<TeacherBean> data) {
            this.data = data;
        }
    }
}
